<?php
/**
 * 登录后台控制器
 */

 class LoginAction extends Action {
	public function index() {
		$this->display('login');
	}

	public function verify() {
		import('ORG.Util.Image');
		Image::buildImageVerify(4, 1);
	}

	public function login() {
		$username = I('username');
		$password = I('password', '', 'md5');
		$school_token = I('school_token');

		$user = M('school_alumni_verify_admin')->where(array('username' => $username, 'password' => $password, 'school_token' => $school_token))->find();
		if($user) {
		$data = array(
			'id' => $user['id'],
			'logintime' => time(),
			'loginip' => get_client_ip(),
		);

		M('school_alumni_verify_admin')->save($data);
		session('uid', $user['id']);
		session('username', $user['username']);
		session('school_token', $user['school_token']);
		session('logintime', date('y-m-d H:i:s', $user['logintime']));
		session('loginip', $user['loginip']);

		$this->redirect('Index/Index/index');
		} else {
			_404('请先通过验证！！');
		}
	}

	public function logout() {
		session_unset();
		session_destroy();
		$this->redirect('Index/Login/index');
	}

	public function checkcode() {
		if(!IS_AJAX) {
			_404('你是不是来错地方了呢....');
		}

		if (I('code', '' ,'md5') != session('verify')) {
		 $this->ajaxReturn(array('status' => 2,'msg' => '验证码错误'), 'json');
		} else {
		 $this->ajaxReturn(array('status' => 0, 'msg' => '验证成功'), 'json');
		}
	}

	public function checkUsername() {
		if(!IS_AJAX) {
		 _404('你是不是来错地方了呢....');
		}

		$username = I('username');
		$user = M('school_alumni_verify_admin')->where(array('username' => $username))->find();
		if (!$user) {
		 $this->ajaxReturn(array('status' => 2,'msg' => '用户名不存在'), 'json');
		} else if ($user['lock']) {
		 $this->ajaxReturn(array('status' => 2,'msg' => '该用户被锁定'), 'json');
		} else {
		 $this->ajaxReturn(array('status' => 0,'msg' => '正常'), 'json');
		}
	}

	public function checkPassword() {
	 if(!IS_AJAX) {
		 _404('你是不是来错地方了呢....');
		}

		$username = I('username');
		$password = I('password', '', 'md5');

		$user = M('school_alumni_verify_admin')->where(array('username' => $username, 'password' => $password))->find();
		if (!$user) {
		 $this->ajaxReturn(array('status' => 2,'msg' => '密码错误'), 'json');
		} else if ($user['lock']) {
		 $this->ajaxReturn(array('status' => 2,'msg' => '该用户被锁定'), 'json');
		} else {
		 $this->ajaxReturn(array('status' => 0,'msg' => '正常'), 'json');
		}
	}
 }